<template>
  <a-modal :title="title" :visible="upVersionVisible" @cancel="cancel" @ok="ok">
    <!-- upVersionFormDatas.app_type == 1 ? 'wgt资源包' : `${upVersionFormDatas.app_type == 0 ? 'App安装包' : '修改'}` -->
    <a-form-model
      class="fromModels"
      ref="editupVersionModel"
      :model="upVersionFormDatas"
      layout="vertical"
      :labelCol="{ span: 6 }"
      :wrapperCol="{ span: 16 }"
    >
      <a-form-model-item label="更新标题" prop="title">
        <a-input v-model="upVersionFormDatas.title" placeholder="请输入标题" />
      </a-form-model-item>
      <a-form-model-item label="更新内容" prop="content">
        <a-input v-model="upVersionFormDatas.content" placeholder="请输入标题" />
      </a-form-model-item>
      <a-form-model-item
        label="平台"
        prop="platform"
        :rules="[{ required: true, message: '请选择平台', trigger: 'change' }]"
      >
        <a-radio-group v-model="upVersionFormDatas.platform" @change="onChange">
          <a-radio v-for="item in platforms" :key="item.id" :value="item.id"> {{ item.name }} </a-radio>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item
        label="应用版本名称"
        :rules="[{ required: true, message: '请输入应用版本名称', trigger: 'blur' }]"
      >
        <a-input v-model="upVersionFormDatas.show_version" placeholder="请输入标题" />
      </a-form-model-item>
      <a-form-model-item label="应用版本号" :rules="[{ required: true, message: '请输入应用版本号', trigger: 'blur' }]">
        <a-input v-model="upVersionFormDatas.version" placeholder="请输入标题" />
      </a-form-model-item>
      <a-form-model-item label="上传apk/ipa包" prop="title">
        <upImage :urls.sync="upVersionFormDatas.title"></upImage>
        <!-- <a-input v-model="upVersionFormDatas.title"  placeholder="请输入标题" /> -->
      </a-form-model-item>
      <a-form-model-item
        label="包地址"
        prop="url"
        :rules="[{ required: true, message: '请输入包地址', trigger: 'blur' }]"
      >
        <a-input v-model="upVersionFormDatas.url" placeholder="请输入标题" />
      </a-form-model-item>
      <a-form-model-item label="强制更新" prop="is_force">
        <a-switch v-model="upVersionFormDatas.is_force" />
      </a-form-model-item>
      <a-form-model-item label="内测发行" prop="type">
        <a-switch v-model="upVersionFormDatas.type" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { editListAPI, addListAPI } from '@/api/system.js'
import upImage from '@/components/upImage'
export default {
  props: ['platforms'],
  components: {
    upImage
  },
  data() {
    return {
      title: '',
      upVersionVisible: false,
      upVersionFormDatas: {
        brand_id: process.env.VUE_APP_BRAND_ID,
        app_type: 1,
        brand_name: '',
        content: '',
        is_force: 0,
        platform: undefined,
        show_version: '',
        status: 1,
        title: '',
        type: false,
        url: '',
        version: ''
      }
    }
  },
  watch: {
    upVersionVisible(boo) {
      if (!boo) {
        this.$refs.editupVersionModel.resetFields()
      }
    }
  },
  created() {},
  methods: {
    onChange(value) {
      console.log(value)
    },
    openupVersionDiogla(json, record) {
      if (json == 1) {
        this.title = 'wgt资源包'
      } else if (json == 0) {
        this.title = 'App安装包'
      } else {
        this.title = '修改'
      }
      if (Object.values(record).length > 0) {
        this.mapObject(record)
        this.upVersionFormDatas.app_type = record.app_type
        this.upVersionFormDatas.ids = record.id
        this.upVersionFormDatas.brand_name = record.brand_name
        this.upVersionFormDatas.content = record.content
        this.upVersionFormDatas.is_force = record.is_force
        this.upVersionFormDatas.platform = record.platform
        this.upVersionFormDatas.show_version = record.show_version
        this.upVersionFormDatas.status = record.status
        this.upVersionFormDatas.title = record.title
        this.upVersionFormDatas.type = record.type
        this.upVersionFormDatas.url = record.url
        this.upVersionFormDatas.version = record.version
      } else {
        this.upVersionFormDatas = {
          brand_id: process.env.VUE_APP_BRAND_ID,
          app_type: json,
          brand_name: '',
          content: '',
          is_force: false,
          platform: undefined,
          show_version: '',
          status: 1,
          title: '',
          type: false,
          url: '',
          version: ''
        }
      }

      this.upVersionVisible = true
    },

    handleOk() {
      this.upVersionVisible = false
    },
    cancel() {
      this.handleOk()
    },
    mapObject(object) {
      if (object.type == 1) {
        object.type = true
      }
      if (object.type == 0) {
        object.type = false
      }
      if (object.is_force == 1) {
        object.is_force = true
      }
      if (object.is_force == 0) {
        object.is_force = false
      }
      return object
    },
    upObject(object) {
      if (object.type) {
        object.type = 1
      } else {
        object.type = 0
      }
      if (object.is_force) {
        object.is_force = 1
      } else {
        object.is_force = 0
      }
      return object
    },
    async ok() {
      if (this.title == '修改') {
        const upForm = this.upObject(JSON.parse(JSON.stringify(this.upVersionFormDatas)))
        await editListAPI(upForm)
        this.$emit('upload')
        this.$message.success('操作成功')
        this.handleOk()
      } else {
        this.$refs.editupVersionModel.validate(async valid => {
          if (!valid) return
          const upForm = this.upObject(JSON.parse(JSON.stringify(this.upVersionFormDatas)))
          console.log(upForm, 123456)
          await addListAPI(upForm)
          this.$emit('upload')
          this.$message.success('操作成功')
          this.handleOk()
        })
      }
      // writeBankAstrictListApi({
      //   astrict_data:this.astrict_data,
      //   bank_id:this.bank_id,
      // }).then(res=>{
      //   this.$message.success("操作成功");
      //   this.handleOk()
      //   this.$emit("changes")
      // })
    }
  }
}
</script>

<style lang="less" scoped>
.cardBlock {
  .cardBlock_title {
    padding: 10px 0;
    margin: 0 0 5px;
    color: rgba(0, 0, 0, 0.85);
  }
  .cardBlock_fors {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    .cardBlock_items {
      width: 45%;
      display: flex;
      align-items: center;
      .cardBlock_items_titles {
        color: rgba(0, 0, 0, 0.65);
      }
      .cardBlock_items_lable {
        border: 1px solid #d9d9d9;
        padding: 4px 0;
        border-radius: 2px;
        width: 220px;
        text-align: center;
      }
    }
  }
}
</style>
